new Ext.Application({
    name: 'iExp',
    icon: 'icon.png',

    phoneStartupScreen: 'phone_startup.png',

    launch: function() {
        
        document.body.addEventListener('touchstart', function(e) {e.preventDefault()}, false);
        
        this.viewport = new Ext.TabPanel({
            fullscreen: true,
            cardSwitchAnimation: {type: 'slide', duration: 400},
            tabBar: {
                dock: 'bottom',
                _scroll: 'horizontal',
                _sortable: true,
                layout: {
                    pack: 'center'
                }
            },
            items: [
    /*            Ext.apply(routesList, {iconCls: 'route', title: 'Маршрут'})
                ,
                {
                    iconCls: 'cash', title: 'Касса',
                    html : '<h1>Тут будет про несданную выручку</h1>'
                }
                ,*/
                Ext.apply(Ext.ControllerManager.get('Navigator').viewport, {iconCls: 'daily', title: 'Справочник'})
                ,
                Ext.apply(syncPanel, {iconCls: 'refresh', title: 'Сервер'})
                ,
                Ext.apply(prefsPanel, {iconCls: 'settings', title: 'Настройки'})
            ]
        }),
        
        dataSync.ajax = new Ext.data.Connection ({
            url: location.protocol+'//'+location.host
                +location.pathname.slice(0,location.pathname.substr(0,location.pathname.length - 1).lastIndexOf('/'))
                +'/XML/?config=datasync'
            , listeners: {
                beforerequest: { fn: syncPanel.newNote },
                requestcomplete: { fn: syncPanel.successNote},
                requestexception : {fn: syncPanel.failureNote}
            }
        });
        
        dataSync.db = dbinit('iExp');
        dataSync.credentials.view = 'iexp';
        
        dataSync.loadSchema(dbSchema);
        
        Ext.each(dataSync.tables, function(t, idx, tables){
            var fieldsArray = [],
                assArray = []
            ;
            
            Ext.each (t.columns, function(c){
                fieldsArray.push({
                    name: c.name,
                    useNull: true,
                    type: (function (dbtype){switch (dbtype.substr(0,3)) {
                        case 'int':
                            return 'int'
                        case 'dec':
                            return 'float'
                    } return 'string'
                    })(c.type)
                })
                
                if (c.fk)
                    assArray.push({
                        type: 'belongsTo',
                        model: c.fk.parent.name,
                        foreignKey: c.name
                    })
                
            })
            
            Ext.each(t.deps, function(dep){
                assArray.push({
                    type: 'hasMany',
                    model: dep.child.name,
                    foreignKey: dep.fk.column.name,
                    name: dep.name,
                    storeConfig:{
                        proxy: {type:'sql', engine:dataSync}
                    }
                })
            })
            
            Ext.regModel(t.name, {
                fields: fieldsArray,
                associations: assArray,
                proxy: {
                    type: 'sql',
                    tableName: t.name,
                    engine: dataSync
                }
            })
        });
        
        document.title=document.title+' '+dbSchema.version;
        
        prefsStore.load();
        
    }
})